package saobracaj;
import java.awt.*;
import java.awt.geom.Ellipse2D;
import java.util.Random;

import javax.swing.JComponent;
import javax.swing.JLabel;

public class Pesak extends Component implements Runnable{
	private Thread nit = new Thread(this);
	private int posX, posY;
	private int speed;
	private Raskrsnica raskrsnica;
	private int brRaskrsnice;
	private Object o = new Object();
	private Color c;
	
	private JLabel label;
	
	
// kretanje pesaka - da se krecu razlicitim brzinama 		

	
	Pesak(int posX, int posY, int br, Raskrsnica raskrsnica, int v){
		//koja pozicija?v

		Random generator = new Random();
		
		int i = generator.nextInt(4);
		switch (i){
		case 0: c = Color.pink;
				break;
		case 1: c = Color.blue;
				break;
		case 2: c = Color.green;
				break;
		case 3: c = Color.orange;
				break;
		}
		speed = v;
		brRaskrsnice = br;
		this.posX = posX;
		this.posY = posY;
		
		label = new JLabel();
		label.setSize(5, 5);
		label.setLocation(posX, posY);
		label.setIcon(new javax.swing.ImageIcon(
				getClass()
				.getResource(
						"/images/pesak.png")));
		
		RaskrsnicaGUI.dohvatiRaskrsnicu().getJLayeredPane().add(label);
		this.raskrsnica = raskrsnica;
		nit.start();
		//update();
		
	}
	
	/*public void paint()
	{
		
		Graphics g2 = RaskrsnicaGUI.dohvatiRaskrsnicu().getJPanel().getGraphics();
		g2.setColor(c);
		
		g2.fillOval(posX, posY, 5, 5);
		
	}
	
	public void repaint(){
		super.repaint();
		//paint();
		
	}
	
	public void update(){
		paint();
	}
	*/

	
	

	
	public void prelazak(){	
		synchronized(o){
			o.notify();
		}
	}
	
	public void predjiUlicu(){
		int n = 100;
		for(int i = 0; i < n; i++)
		{
			try {
				Thread.sleep(100);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
			switch (brRaskrsnice){
			case 0: posX = posX + 2;
					break;
			case 1: posX = posX - 2;
					break;
			case 2: posY = posY + 2;
					break;
			case 3: posY = posY - 2;
					break;
			case 4: posX = posX - 2;
					break;
			case 5: posX = posX + 2;
					break;
			case 6: posY = posY - 2;
					break;
			case 7: posY = posY + 2;
					break;
			}
			label.setLocation(posX, posY);
			//RaskrsnicaGUI.dohvatiRaskrsnicu().repaint();	//FIXME OVO MOZE DA IZAZOVE  BELAJ!
			//paint();
		}
	}
	
	public void run(){
		 synchronized(o){
			try{
				
				switch(brRaskrsnice){
				case 0: case 1: if( raskrsnica.getSeverPes().dohvatiSvetlo() == 3 || raskrsnica.getSeverPes().isTreptanje()) predjiUlicu();
								else {
									o.wait();
									predjiUlicu();
								}
								break;
				case 2: case 3: if( raskrsnica.getIstokPes().dohvatiSvetlo() == 3 || raskrsnica.getIstokPes().isTreptanje()) predjiUlicu();
								else {
									o.wait();
									predjiUlicu();	
								}
								break;
				case 4: case 5: if( raskrsnica.getJugPes().dohvatiSvetlo() == 3 || raskrsnica.getJugPes().isTreptanje()) predjiUlicu();
								else {
								o.wait();
								predjiUlicu();	
								}
								break;
				case 6: case 7: if( raskrsnica.getZapadPes().dohvatiSvetlo() == 3 || raskrsnica.getZapadPes().isTreptanje()) predjiUlicu();
								else {
									o.wait();
									predjiUlicu();	
								}
								break;
				}
				
				switch (brRaskrsnice){
				case 0: label.hide();
						raskrsnica.getPsever1().remove(this);
						break;
				case 1: label.hide();
						raskrsnica.getPsever2().remove(this);
						break;
				case 2: raskrsnica.getPistok1().remove(this);
						label.hide();
						break;
				case 3: raskrsnica.getPistok2().remove(this);
						label.hide();
						break;
				case 4: raskrsnica.getPjug1().remove(this);
						label.hide();
						break;
				case 5: raskrsnica.getPjug2().remove(this);
						label.hide();
						break;
				case 6: raskrsnica.getPzapad1().remove(this);
						label.hide();
						break;
				case 7: raskrsnica.getPzapad2().remove(this);
						label.hide();
						break;
				}
			} 
				catch(InterruptedException e)
				{
					e.printStackTrace();
				};
		}
	} 




}
